package lingou.homework.postbar.controller.tool;

import java.util.function.Supplier;

public class Chain<T> {

    Supplier<? extends T>[] suppliers;

    public static <T> Chain<T> of(Supplier<? extends T>...suppliers){
        Chain<T> chain = new Chain<>(suppliers);
        return chain;
    }

    protected Chain(Supplier<? extends T>[] suppliers){
        this.suppliers = suppliers;
    }

    public T result(){
        return result(null);
    }

    public T result(T f){
        for (Supplier<? extends T> supplier : suppliers) {
            T r = supplier.get();
            if(r != null) return r;
        }
        return f;
    }

}
